package springapp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import springapp.entity.ICacheEntity;

@Service
public class CacheService implements IServiceConstants {

	private Map<String, ICache<?, ?>> nameVsCacheMap = new HashMap<String, ICache<?, ?>>();

	public ICache<?, ?> getCache(String name) {
		return this.nameVsCacheMap.get(name);
	}

	@SuppressWarnings("unchecked")
	public <E extends ICacheEntity> void addToCache(List<E> entities,
			String cacheName) {
		ICache<Integer, String> cache = (ICache<Integer, String>) this
				.getCache(cacheName);
		
		if(cache == null){
			cache = new Cache<Integer, String>();
			this.nameVsCacheMap.put(cacheName, cache);
		}
		
		for (E entity : entities) {
			cache.put(entity.getId(), entity.getName());
		}

	}

}
